package sury.web.controller.back;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sury.biz.service.AdminService;
import sury.common.constant.JwtClaimsConstant;
import sury.common.context.BaseContext;
import sury.common.properties.JwtProperties;
import sury.common.result.Result;
import sury.common.utils.JwtUtil;
import sury.dal.dto.AdminDTO;
import sury.dal.dto.AdminLoginDTO;
import sury.dal.entity.AdminEntity;
import sury.dal.vo.AdminLoginVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 后端接口-管理员
 */
@RestController
@Slf4j
@RequestMapping("/back/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 管理员登录
     * @param adminLoginDTO
     * @return
     * TODO 生成jwt令牌，密码MD5，阿里短信验证码登录，更详细的错误消息提示
     */
    @PostMapping("/login")
    public Result login(@RequestBody AdminLoginDTO adminLoginDTO){
        log.info("admin login..{}",adminLoginDTO);
        AdminEntity adminEntity=adminService.adminLogin(adminLoginDTO);
        AdminLoginVo adminLoginVo= BeanUtil.copyProperties(adminEntity,AdminLoginVo.class);

        //生成jwt
        Map<String, Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.ADMIN_ID,adminEntity.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(),jwtProperties.getAdminTtl(),claims);
        adminLoginVo.setToken(token);

        return Result.success(adminLoginVo);
    }

    /**
     * 获取当前用户信息
     * @param
     * @return
     */
    @GetMapping("/current-admin")
    public Result getCurrentAdmin(){
        Long adminId=BaseContext.getCurrentId();
        AdminEntity adminEntity=adminService.getById(adminId);
        return Result.success(adminEntity);
    }

    /**
     * 分页查询管理员
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/page")
    public Result pageAdmin(@RequestParam(value = "current",defaultValue = "1") Long current,
                            @RequestParam(value = "size",defaultValue = "10") Long size,
                            @RequestParam(value = "name",required = false) String name){
        Page<AdminEntity> page=new Page<>(current,size);
        return Result.success(adminService.pageAdmin(page,name));
    }

    /**
     * 根据id查询管理员
     * @param id
     * @return
     * TODO：用户不存在
     */
    @GetMapping("/{id}")
    public Result queryAdminById(@PathVariable("id") Long id){
        return Result.success(adminService.getById(id));
    }

    /**
     * 增加管理员
     * @param adminDTO
     * @return
     * TODO：登录退出
     * TODO:默认avatar、role；create_time/user、update_time/user；status
     * TODO：权限
     */
    @PostMapping
    public Result saveAdmin(@RequestBody AdminDTO adminDTO){
        adminService.saveAdmin(adminDTO);
        return Result.success();
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteAdminById(@PathVariable("id") Long id){
        adminService.removeById(id);
        return Result.success();
    }

    /**
     * 更新管理员
     * @param id
     * @param adminDTO
     * @return
     */
    @PostMapping("/update/{id}")
    public Result updateAdmin(@PathVariable("id") Long id,@RequestBody AdminDTO adminDTO){
        adminService.updateById(id,adminDTO);
        return Result.success();
    }

    /**
     * 给管理员分配角色
     * @param id
     * @param roleIds
     * @return
     */
    @PostMapping("/role/update")
    public Result assignRolesToUser(@RequestParam("id") Long id,@RequestParam("roleIds") List<Long> roleIds){
        adminService.assignRolesToUser(id,roleIds);
        return Result.success();
    }

    /**
     * 获取指定用户的角色
     * @param id
     * @return
     */
    @GetMapping("/role/{id}")
    public Result getRoleList(@PathVariable("id") Long id){
        return Result.success(adminService.getRoleList(id));
    }

}